package com.kindel.service.weixin.impl;

import com.common.model.view.wechat.applet.JsCode2SessionResView;
import com.common.model.view.wechat.applet.WechatAppletUserInfo;
import com.kindel.mapper.weixin.WeiXinMapper;
import com.kindel.service.weixin.WeixinService;
import com.rscache.cache.Rscache;
import com.rscache.cache.RscacheEvict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by demongao on 2017/9/6.
 */
@Service
public class WeixinServiceImpl implements WeixinService {

    @Autowired
    private WeiXinMapper weiXinMapper;

    //3rd_session
    @RscacheEvict(key = "kindlecp:session:3rd:%s", expr = "#session_3rd")
    @Override
    public void removeSession3rdFromRedis(String session_3rd) {

    }

    @Rscache(key = "kindlecp:session:3rd:%s", expr = "#session_3rd", expire = 60 * 60 * 24 * 29, resetExpire = true)
    @Override
    public JsCode2SessionResView saveSession3rdToRedis(String session_3rd, JsCode2SessionResView jsCode2SessionResView) {
        return jsCode2SessionResView;
    }

    @Rscache(key = "kindlecp:session:3rd:%s", expr = "#session_3rd", expire = 60 * 60 * 24 * 29, resetExpire = true)
    @Override
    public JsCode2SessionResView loadSession3rdFromRedis(String session_3rd) {
        return null;
    }

    @Rscache(key = "kindlecp:wechat_applet:user_info:%s", expr = "#session_3rd", expire = 60 * 60 * 24 * 29, resetExpire = true)
    @Override
    public WechatAppletUserInfo saveWechatAppletUserInfoToRedis(String session_3rd, WechatAppletUserInfo userInfo) {
        return userInfo;
    }
    @Rscache(key = "kindlecp:wechat_applet:user_info:%s", expr = "#session_3rd", expire = 60 * 60 * 24 * 29, resetExpire = true)
    @Override
    public WechatAppletUserInfo loadWechatAppletUserInfoFromRedis(String session_3rd) {
        return null;
    }
    @RscacheEvict(key = "kindlecp:wechat_applet:user_info:%s", expr = "#session_3rd")
    @Override
    public void removeWechatAppletUserInfoFromRedis(String session_3rd) {


    }

    @Override
    public boolean saveOrUpdateWechatAppletUserInfo(WechatAppletUserInfo wechatAppletUserInfo) {

        //查询用户是否存在 （根据openId 判断）

        if(weiXinMapper.loadWeiXinUserInfoCountByOpenId(wechatAppletUserInfo.getOpenId())!=0){
            System.out.println(wechatAppletUserInfo.toString());
            weiXinMapper.updateWeiXinUserInfo(wechatAppletUserInfo);
        }else{
            weiXinMapper.saveWeiXinUserInfo(wechatAppletUserInfo);
        }

        return true;
    }
}
